<template>
  <div class="app-container">
    <!-- 接口状态提示 -->


    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="积分规则ID" label-width="70" prop="ruleId">
        <el-input
          v-model="queryParams.ruleId"
          placeholder="请输入积分规则ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['point:flow:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['point:flow:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['point:flow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['point:flow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="65" align="center" />
      <el-table-column label="流水ID" align="center" prop="id" width="150" />
      <el-table-column label="用户名称" align="center" width="200">
        <template slot-scope="scope">
          <span style="margin-left: 8px;">{{ (scope.row.users && scope.row.users.nickname) || '未知用户' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" width="200">
        <template slot-scope="scope">
          <el-tag
            :type="getSourceTagType(scope.row.pointRule && scope.row.pointRule.typeName)"
            size="small"
          >
            {{ (scope.row.pointRule && scope.row.pointRule.typeName) || '未知来源' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="积分变动值" align="center" prop="point" width="200">
        <template slot-scope="scope">
          <span :class="scope.row.point >= 0 ? 'text-success' : 'text-danger'">
            {{ scope.row.point >= 0 ? '+' : '' }}{{ scope.row.point }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作后剩余积分" align="center" prop="balance" width="150" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="250">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['point:flow:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['point:flow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="积分变动值" prop="point">
          <el-input v-model="form.point" placeholder="请输入积分变动值" />
        </el-form-item>
        <el-form-item label="操作后剩余积分" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入操作后剩余积分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看积分流水详情对话框 -->
    <el-dialog title="积分流水详情" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="流水ID">{{ viewForm.id }}</el-descriptions-item>
        <el-descriptions-item label="用户名称">{{ viewForm.users && viewForm.users.nickname }}</el-descriptions-item>
        <el-descriptions-item label="来源">{{ viewForm.pointRule && viewForm.pointRule.typeName }}</el-descriptions-item>
        <el-descriptions-item label="积分变动值">
          <span :class="viewForm.point >= 0 ? 'text-success' : 'text-danger'">
            {{ viewForm.point >= 0 ? '+' : '' }}{{ viewForm.point }}
          </span>
        </el-descriptions-item>
        <el-descriptions-item label="操作后剩余积分">{{ viewForm.balance }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(viewForm.createTime) }}</el-descriptions-item>
      </el-descriptions>

      <!-- 关联信息展示 -->
<!--      <el-divider content-position="left">关联信息</el-divider>-->
      <el-row :gutter="20">
        <el-col :span="12" v-if="viewForm.comments && viewForm.comments.content">
          <el-card shadow="hover">
            <div slot="header">
              <span>评论信息</span>
            </div>
            <p><strong>内容:</strong> {{ viewForm.comments.content }}</p>
            <p><strong>目标类型:</strong> {{ viewForm.comments.targetType }}</p>
            <p><strong>目标ID:</strong> {{ viewForm.comments.targetId }}</p>
          </el-card>
        </el-col>
        <el-col :span="12" v-if="viewForm.exchangeOrder && viewForm.exchangeOrder.orderSn">
          <el-card shadow="hover">
            <div slot="header">
              <span>兑换订单</span>
            </div>
            <p><strong>订单号:</strong> {{ viewForm.exchangeOrder.orderSn }}</p>
            <p><strong>商品名称:</strong> {{ viewForm.exchangeOrder.goodsName }}</p>
          </el-card>
        </el-col>
        <el-col :span="12" v-if="viewForm.recipes && viewForm.recipes.title">
          <el-card shadow="hover">
            <div slot="header">
              <span>菜谱信息</span>
            </div>
            <p><strong>标题:</strong> {{ viewForm.recipes.title }}</p>
            <p><strong>简介:</strong> {{ viewForm.recipes.introduction || '无' }}</p>
          </el-card>
        </el-col>
        <el-col :span="12" v-if="viewForm.works && viewForm.works.title">
          <el-card shadow="hover">
            <div slot="header">
              <span>作品信息</span>
            </div>
            <p><strong>标题:</strong> {{ viewForm.works.title }}</p>
            <p><strong>内容:</strong> {{ viewForm.works.content || '无' }}</p>
          </el-card>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { listFlow, getFlow, delFlow, addFlow, updateFlow, listFlowComplete, getFlowCompleteDetail, listFlowOptimized } from "@/api/point/flow"

export default {
  name: "Flow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 积分流水表格数据
      flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看弹出层
      viewOpen: false,
      // 查看的表单数据
      viewForm: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        ruleId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        ruleId: [
          { required: true, message: "积分规则ID不能为空", trigger: "blur" }
        ],
        point: [
          { required: true, message: "积分变动值不能为空", trigger: "blur" }
        ],
        balance: [
          { required: true, message: "操作后剩余积分不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询积分流水列表 */
    getList() {
      this.loading = true
      // 使用优化后的接口获取完整信息
      listFlowOptimized(this.queryParams).then(response => {
        this.flowList = response.rows || response.data
        this.total = response.total || response.data.length
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        ruleId: null,
        point: null,
        balance: null,
        createTime: null,
        del: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加积分流水"
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.viewForm = row
      this.viewOpen = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getFlow(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改积分流水"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFlow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addFlow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除积分流水编号为"' + ids + '"的数据项？').then(function() {
        return delFlow(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('point/flow/export', {
        ...this.queryParams
      }, `flow_${new Date().getTime()}.xlsx`)
    },
    /** 获取来源标签类型 */
    getSourceTagType(typeName) {
      if (!typeName) return 'info'
      if (typeName.includes('购买') || typeName.includes('消费')) return 'danger'
      if (typeName.includes('发布') || typeName.includes('获得')) return 'success'
      if (typeName.includes('评论')) return 'warning'
      if (typeName.includes('兑换')) return 'info'
      return 'primary'
    }
  }
}
</script>

<style scoped>
.text-success {
  color: #67c23a;
  font-weight: bold;
}
.text-danger {
  color: #f56c6c;
  font-weight: bold;
}
.el-avatar {
  vertical-align: middle;
}
</style>
